import 'package:flutter/material.dart';
import 'package:hzcloud_flt/common/hz_loading/hz_default_status.dart';

typedef AsyncChildBuilder<T> = Widget Function(BuildContext context, T data);

class HZBaseLoading<T> extends StatelessWidget {
  final Future<T> future;
  final AsyncChildBuilder builder;
  final Widget errorWidget;

  const HZBaseLoading(
      {Key key,
      @required this.future,
      @required this.builder,
      this.errorWidget})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return FutureBuilder(
      future: future,
      initialData: Text('loading'),
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
            return new Text(
                'Press button to start'); //如果_calculation未执行则提示：请点击开始
          case ConnectionState.waiting:
            return HZDefaultStatus(
              status: HZBaseLoadingStatus.loading,
            ); //如果_calculation正在执行则提示：加载中
          default:
            //如果_calculation执行完毕
            print(snapshot.data);
            if (snapshot.hasError||snapshot.data == null) {
              return HZDefaultStatus(
                status: HZBaseLoadingStatus.requestFailed,
              );
            } else {
              return builder(context, snapshot.data);
            }
        }
      },
    );
  }
}
